package com.timothy.android.answer;


import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

import com.timothy.android.BaseProcessActivity;
import com.timothy.android.R;
import com.timothy.android.view.Bar2DFFView;
import com.timothy.android.view.BaseBean;
import com.timothy.service.MessageSenderUtil;
import com.timothy.statistics.AnswerBuilder;

public class Bar2DFFActivity extends BaseProcessActivity{
	private static final String LOG_TAG = "Bar2DFFActivity";
	
//	protected ButtonStatistics answer = new ButtonStatistics();
//	List<ChoiceResult> barList = new ArrayList<ChoiceResult>();
//	Integer barListSize = null;
	
	BaseBean baseBean;
	Bar2DFFView bar2DFFView;
//	private GestureDetector detector = null;
	
//	protected String[] chartColorArr = new String[]{"#ff0000","#ff7f00"};
//	protected String[] textDesc = new String[]{"Collect","InCollect"};
	
	//header component(left, center,right)
	TextView answerDesc;
	Button nextBtn;
	
	public void setHeader(Integer type) {
		//set answerDesc
		answerDesc = (TextView) this.findViewById(R.id.answerDesc);
		answerDesc.setText("Q " + baseBean.getQuestionIndex().toString()+" of " + baseBean.getTotalQuestionCount().toString());

		//set next button
		if(baseBean.getQuestionIndex() == baseBean.getTotalQuestionCount()){
			ShowDialog("Vote Ended ! Please visit www.powercomars.com for more product information.");
		}else{
			if (MessageSenderUtil.MODE != null && MessageSenderUtil.MODE == 2) {
				nextBtn = (Button) this.findViewById(R.id.nextBtn);
				nextBtn.setVisibility(View.VISIBLE);
				nextBtn.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v) {
						sendNextQuestion();
					}
				});
			}
		}
	}
	
	//send answers to server
	public void sendNextQuestion(){
		Log.i(LOG_TAG, "sendNextQuestion()...");
		String message = MessageSenderUtil.sendNextQuestion();
		Log.i(LOG_TAG, "message:"+message);
		if(!sendMessage(message)){
			ShowDialog("Can not connect to pc, please confirm PC is running!");
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.answer_2d_ff_bar);
		
		Bundle bundle = this.getIntent().getExtras();
		String message = bundle.getString("message");
//		Log.i(LOG_TAG, "message:"+message);
//		
//		answer = StatisticsBuilder.parseFreeFormStatistics(message);
//		BaseQuestion bq = answer.getQuestion();
//		barList = answer.getChoiceResults();
//		barListSize = barList.size();
//		
		
//		SurfaceView view = new VerticalBar2DView(this);
//		setContentView(view);
		
		baseBean = AnswerBuilder.parseBaseBeanForFreeForm(message);
		
		//set header
		setHeader(baseBean.getQuestionType());
		
		//get bar2DVerView and set bar2DVerView
		bar2DFFView = (Bar2DFFView) this.findViewById(R.id.bar2DFFView);
//		bar2DFFView.setOnTouchListener(this);
		bar2DFFView.setLongClickable(true);
		bar2DFFView.setSaveEnabled(false);
		
		bar2DFFView.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				recoverBrightness();
//				detector.onTouchEvent(arg1);
				return false;
			}
		});
		
		bar2DFFView.setBaseBean(baseBean);
		bar2DFFView.setPage(1);
		bar2DFFView.setScreenSize(screenSize);
		
//		detector = new GestureDetector((OnGestureListener) this);
        
	}
	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

}
